package com.example.api.service.impl;

import com.example.api.dao.EmployeeDao;
import com.example.api.dao.WarehouseDao;
import com.example.api.model.entity.Employee;
import com.example.api.model.entity.Warehouse;
import com.example.api.model.support.RespBean;
import com.example.api.service.WarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseDao warehouseDao;

    @Resource
    private EmployeeDao employeeDao;

    @Override
    public RespBean save(Warehouse warehouse) {
        Warehouse result = warehouseDao.selectByName(warehouse.getName().trim());
        if (Objects.nonNull(result)) {
            return RespBean.error("仓库名称重复");
        }
        warehouse.setCreateTime(new Date());
        if (warehouseDao.insertSelective(warehouse) > 0) {
            Employee employee = new Employee();
            employee.setId(warehouse.getEid());
            employee.setDepartment(warehouse.getId());
            employee.setUpdateTime(new Date());
            employeeDao.updateByPrimaryKeySelective(employee);
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @Override
    public List<Warehouse> findAll() {
        return warehouseDao.findAll();
    }

    @Override
    public RespBean delete(Integer id) {
        if (warehouseDao.deleteByPrimaryKey(id) > 0) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

}